package com.wangjiacai.utils.web.validator;

import com.wangjiacai.utils.enums.ResultEnum;
import org.springframework.util.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = SecureNotBlank.NotBlankValidator.class)
@SecureValid(errorMessageBy = SecureNotBlank.NotBlankErrorMessage.class)
public @interface SecureNotBlank {

    String message() default "{javax.validation.constraints.NotBlank.message}";

    ResultEnum result() default ResultEnum.ParamsMissing;

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    class NotBlankValidator implements ConstraintValidator<SecureNotBlank,String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
            if(!StringUtils.isEmpty(value)) return true;
            return false;
        }
    }

    class NotBlankErrorMessage implements SecureValid.ErrorMessage<SecureNotBlank>{
        @Override
        public ResultEnum resultEnum(SecureNotBlank secureNotBlank) {
            return secureNotBlank.result();
        }
    }
}
